home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / portfoli / bootst11.lzh / PF BOOTSTRAP VATIPX PUUCPLOCKC < prev    next >
Text File  |  1991-05-02  |  3KB  |  100 lines

  1. /*
  2.  * Copyright (c) 1988 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #ifndef lint
  19. static char sccsid[] = "@(#)uucplock.c    5.3 (Berkeley) 9/2/88";
  20. #endif /* not lint */
  21.  
  22. #include <sys/types.h>
  23. #include <sys/file.h>
  24. #include <sys/dir.h>
  25. #include <errno.h>
  26.  
  27. /* pick the directory naming scheme you are using */
  28.  
  29. #define LOCKDIRNAME    "/usr/spool/uucp/LCK..%s"    /**/
  30. /* #define LOCKDIRNAME    "/usr/spool/uucp/LCK/LCK..%s"    /**/
  31.  
  32. /* 
  33.  * uucp style locking routines
  34.  * return: 0 - success
  35.  *       -1 - failure
  36.  */
  37.  
  38. uu_lock(ttyname)
  39.     char *ttyname;
  40. {
  41.     extern int errno;
  42.     int fd, pid;
  43.     char tbuf[sizeof(LOCKDIRNAME) + MAXNAMLEN];
  44.     off_t lseek();
  45.  
  46.     (void)sprintf(tbuf, LOCKDIRNAME, ttyname);
  47.     fd = open(tbuf, O_RDWR|O_CREAT|O_EXCL, 0660);
  48. #if 0
  49.     if (fd < 0) {
  50.         /*
  51.          * file is already locked
  52.          * check to see if the process holding the lock still exists
  53.          */
  54.         fd = open(tbuf, O_RDWR, 0);
  55.         if (fd < 0) {
  56.             perror("lock open");
  57.             return(-1); 
  58.         }
  59.         if (read(fd, &pid, sizeof(pid)) != sizeof(pid)) {
  60.             (void)close(fd);
  61.             perror("lock read");
  62.             return(-1);
  63.         }
  64.  
  65.         if (kill(pid, 0) == 0 || errno != ESRCH) {
  66.             (void)close(fd);    /* process is still running */
  67.             return(-1);
  68.         }
  69.         /*
  70.          * The process that locked the file isn't running, so
  71.          * we'll lock it ourselves
  72.          */
  73.         if (lseek(fd, 0L, L_SET) < 0) {
  74.             (void)close(fd);
  75.             perror("lock lseek");
  76.             return(-1);
  77.         }
  78.         /* fall out and finish the locking process */
  79.     }
  80.     pid = getpid();
  81.     if (write(fd, (char *)&pid, sizeof(pid)) != sizeof(pid)) {
  82.         (void)close(fd);
  83.         (void)unlink(tbuf);
  84.         perror("lock write");
  85.         return(-1);
  86.     }
  87. #endif
  88.     (void)close(fd);
  89.     return(0);
  90. }
  91.  
  92. uu_unlock(ttyname)
  93.     char *ttyname;
  94. {
  95.     char tbuf[sizeof(LOCKDIRNAME) + MAXNAMLEN];
  96.  
  97.     (void)sprintf(tbuf, LOCKDIRNAME, ttyname);
  98.     return(unlink(tbuf));
  99. }
  100.